package com.fl.iocaop.java8;

import org.junit.Test;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class TestDateTime {
    private DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddH");
    @Test
    public void test(){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("toString = " + now);

        //当前时间减少3年
        System.out.println("now.minusYears(3) = " + now.minusYears(3));

        //当前时间加2年
        System.out.println("now.plusYears(2) = " + now.plusYears(2));
        System.out.println("now.getDayOfWeek() = " + now.getDayOfWeek());

        //计算年龄

        LocalDate today = LocalDate.now();
        LocalDate playerDate = LocalDate.from(DateTimeFormatter.ofPattern("yyyy-MM-dd").parse("1998-08-08"));
        long years = ChronoUnit.YEARS.between(playerDate, today);
        System.out.println("years = " + years);
    }
}
